package com.beamfield.controller.api;

import java.io.BufferedReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import com.beamfield.model.PersonInfo;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONObject;
import com.beamfield.common.ApiResult;
import com.beamfield.enums.HttpResultEnum;
import com.beamfield.modelVo.ApiAttendVo;
import com.beamfield.modelVo.ApiAttendVoArray;
import com.beamfield.service.api.ApiAttendService;

@Controller
@RequestMapping("/api")
public class ApiAttendController {

	@Resource
	ApiAttendService apiAttendService;

	/**
	 * 对外提供考勤记录
	 *
	 */
	@RequestMapping("/attend")
	@ResponseBody
	public ApiResult attend(HttpServletRequest req) {
		BufferedReader br;
		try {
			br = req.getReader();
			String str, wholeStr = "";
			while((str = br.readLine()) != null){
			         wholeStr += str;
			}
			List<ApiAttendVo> attendlist = JSONObject.parseObject(wholeStr,ApiAttendVoArray.class).getData();
			apiAttendService.savePerson(attendlist);
		} catch (Exception e) {
			e.printStackTrace();
			return new ApiResult(HttpResultEnum.EXCEPTION.getCode(),HttpResultEnum.EXCEPTION.getMessage());
		}
		return new ApiResult();
	}

	@RequestMapping("/train")
	@ResponseBody
	public ApiResult train(ApiAttendVo attendVo){
		List<ApiAttendVo> apiAttendVoList = new ArrayList<>();
		apiAttendVoList.add(attendVo);
		apiAttendService.trainPunch(apiAttendVoList);
		return new ApiResult();
	}

}
